package com.yqg.manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.controller.dto.ArchiveDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IArchiveService;
import com.yqg.manage.entity.Archive;

import org.springframework.web.bind.annotation.RestController;

/**
 * ArchiveController 前端控制器
 * @author suzhuang
 * @since 2023-07-05
 */
@RestController
@RequestMapping("/archive")
public class ArchiveController {

    @Resource
    private IArchiveService archiveService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody Archive archive) {
        if(archive.getId()!=null){
            archive.setUpdateTime(new Date());
        }
        archive.setIsDelete("0");
        return Result.success(archiveService.saveOrUpdate(archive));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Archive archive =archiveService.getById(id);
        archive.setIsDelete("1");
        archive.setUpdateTime(new Date());
        return Result.success(archiveService.saveOrUpdate(archive));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Archive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<Archive> list=  archiveService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(archiveService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Archive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(archiveService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     *  阶段名
     */
    @GetMapping("/stageName")
    @ApiOperation("全部阶段")
    public Result stageName(){
       return Result.success(archiveService.stageName());
    }

    /**
     *  文书名
     */
    @GetMapping("/documentName")
    @ApiOperation("通过文书类型(外部1，内部0)，阶段id获取文书id、名称")
    public Result documentName(int documentType, int pstageId){
        return Result.success(archiveService.documentName(documentType,pstageId));
    }

    /**
     *  获取documentName和templateNo
     */
    @GetMapping("/documentTemp")
    @ApiOperation("通过文书类型(外部1，内部0)，阶段id获取获取documentName和templateNo")
    public Result documentTemp(int documentType, int pstageId){
        return Result.success(archiveService.documentTemp(documentType,pstageId));
    }

    /**
     *通过文书类型(外部1，内部0)，阶段id获取
     * 文书名称，文书编号，文件名称，更新时间
     * 文件描述，是否删除 (0否1是)
     */
    @GetMapping("/documentlist")
    @ApiOperation("通过文书类型(外部1，内部0)，阶段id获取文书id、名称")
    public Result documentlist(int documentType, int pstageId,
                               @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pageSize) {
        Page<ArchiveDTO> archiveDTOList = archiveService.getDocumentList(documentType, pstageId, page, pageSize);
        return Result.success(archiveDTOList);
    }
}

